﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; 

namespace oqis.core.common.winsys
{
    /// <summary>
    /// 获取系统设备的相关操作
    /// 如获取网卡、显示器等
    /// </summary>
    public class SysDeviceOperator
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public SysDeviceOperator()
        {

        }

        /// <summary>
        /// 获取计算机的网卡信息
        /// </summary>
        public void GetNetworkCardInfo()
        {
            // 得到计算机名称
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports");

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hwnd"></param>
        public MonitorInfo GetMonitorInfo(IntPtr hwnd)
        {
            MonitorInfo mi;

            // 显示器
            IntPtr hMonitor = NativeMethods.MonitorFromWindow(hwnd, NativeConstants.MONITOR_DEFAULTTOPRIMARY);
            int lastWin32Error = Marshal.GetLastWin32Error();

            // 显示器数量
            uint pdwNumberOfPhysicalMonitors = 0u;
            bool numberOfPhysicalMonitorsFromHmonitor = NativeMethods.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, ref pdwNumberOfPhysicalMonitors);
            lastWin32Error = Marshal.GetLastWin32Error();
            mi.MonitorCount = (int)pdwNumberOfPhysicalMonitors;

            // 物理显示器
            NativeStructures.PHYSICAL_MONITOR[] pPhysicalMonitorArray = new NativeStructures.PHYSICAL_MONITOR[pdwNumberOfPhysicalMonitors];
            bool physicalMonitorsFromHmonitor = NativeMethods.GetPhysicalMonitorsFromHMONITOR(hMonitor, pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray);
            lastWin32Error = Marshal.GetLastWin32Error();
            mi.PhysicalMonitorArray = pPhysicalMonitorArray;

            uint pdwMonitorCapabilities = 0u;
            uint pdwSupportedColorTemperatures = 0u;
            var monitorCapabilities = NativeMethods.GetMonitorCapabilities(pPhysicalMonitorArray[0].hPhysicalMonitor, ref pdwMonitorCapabilities, ref pdwSupportedColorTemperatures);
            lastWin32Error = Marshal.GetLastWin32Error();
            mi.MonitorCapabilities = (int)pdwMonitorCapabilities;

            NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE type = NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE.MC_SHADOW_MASK_CATHODE_RAY_TUBE;
            var monitorTechnologyType = NativeMethods.GetMonitorTechnologyType(pPhysicalMonitorArray[0].hPhysicalMonitor, ref type);
            lastWin32Error = Marshal.GetLastWin32Error();

            mi.DisplayType = type;

            //  var destroyPhysicalMonitors = NativeMethods.DestroyPhysicalMonitors(pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray);
            // lastWin32Error = Marshal.GetLastWin32Error();


            return mi;
        }

        /// <summary>
        /// 计算屏幕的长宽尺度
        /// </summary>
        /// <param name="screenValue">屏幕大小，通常单位为英寸</param>
        /// <param name="scaleLength">屏幕长宽比例中的长</param>
        /// <param name="scaleWidth">屏幕长宽比例中的宽</param>
        /// <returns>返回单位为mm的值</returns>
        public System.Drawing.Size GetScreenSize(double screenValue, int scaleLength,int scaleWidth)
        {
            double l, h, v;// 斜边、水平直角边、垂直直角边
            l = screenValue * 25.4;// 将英寸转换成厘米
            double α;// α 为水平锐角，β为垂直方向锐角
            α = scaleWidth / (double)scaleLength; // 得到α角的正切值
            h = Math.Cos(Math.Atan(α)) * l;
            v = Math.Sin(Math.Atan(α)) * l;

            // 返回结果
            return new System.Drawing.Size((int)h, (int)v);
        }
    }

    /// <summary>
    /// 显示器信息
    /// </summary>
    public struct MonitorInfo
    {
        /// <summary>
        /// 数量
        /// </summary>
        public int MonitorCount;

        /// <summary>
        /// 显示器
        /// </summary>
        public NativeStructures.PHYSICAL_MONITOR[] PhysicalMonitorArray;

        /// <summary>
        /// 显示类型
        /// </summary>
        public NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE DisplayType;

        /// <summary>
        /// 
        /// </summary>
        public int MonitorCapabilities;
    }

}